package us.versus.them.glassifried;

import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.display.DisplayObject;

import flash.events.MouseEvent;

class Button extends SimpleButton {

	private var real_up : DisplayObject;
	private var disabled : DisplayObject;

	public function new( x,y, r,g,b, ?text = null,  ?action, ?styleSheet ) {
		super();
		if ( null != text ) {
			this.init( x,y, r,g,b, text, action, styleSheet );
		}
	}

	////

	// h3 should define style for up   text
	// h4 should define style for over text
	public function init( x,y, r,g,b, text, ?action, ?styleSheet ) {
		if ( null == styleSheet ) {
			styleSheet = Glassing.DEFAULT_STYLESHEET;
		}

		var bigF = 1.2;
		var lilF = 0.2;

		var up = Glassing.glassyText( r,g,b, 'h3', text, styleSheet );

		var w2 = up.width  * bigF;
		var h2 = up.height * bigF;
		var tw2 = ( w2 - up.width ) / 2;
		var th2 = ( h2 - up.height ) / 2;

		var over = new Sprite();
		over.addChild( Glassing.glassyText( r,g,b, 'h4', text, styleSheet ) );
		over.x = -tw2;
		over.y = -th2;

		var f = Math.floor;

		var down = Glassing.glassyText( f(r*lilF),f(g*lilF),f(b*lilF), 'h3', text, styleSheet );

		this.disabled = Glassing.glassyText( f(r/4 + 101), f(g/4 + 101), f(b/4 + 101), 'h3', text, styleSheet );

		//var button = new SimpleButton( up , over, down, down );
		var button = this;
		this.real_up      = up;
		this.upState      = up;
		this.overState    = over;
		this.downState    = down;
		this.hitTestState = down;

		button.x = x;
		button.y = y;

		button.filters = Glassing.DROP_FILTER;

		if ( null != action ) {
			var thiz = this;
			button.addEventListener( 
				MouseEvent.CLICK
				, function( e ) { 
					if ( thiz.enabled ) {
						action( text, e ); 
					}
				} 
			);
		}

		return button;
	}

	public static function dupe( displayObject:DisplayObject ) {
		return new flash.display.Bitmap(
			Glassing.displayObjectToBitMapData( displayObject )
		);
	}

	public static function wrap( x,y, r,g,b, displayObject:DisplayObject, ?text, ?action ) {
		var bigF = 1.2;
		var lilF = 0.2;

		var up = Glassing.glassIt( r,g,b, displayObject );

		var w2 = up.width  * bigF;
		var h2 = up.height * bigF;
		var tw2 = ( w2 - up.width ) / 2;
		var th2 = ( h2 - up.height ) / 2;
		var f = Math.floor;


		var over = new Sprite();
		over.addChild( 
			Glassing.glassIt( 
				r,g,b, 
				dupe( displayObject )
			) 
		);
		over.x = -tw2;
		over.y = -th2;

		var down = Glassing.glassIt( f(r*lilF),f(g*lilF),f(b*lilF), dupe( displayObject ) );

		var button = new Button(x,y,r,g,b);
		button.real_up      = up;
		button.upState      = up;
		button.overState    = over;
		button.downState    = down;
		button.hitTestState = down;
		button.disabled = Glassing.glassIt( f(r/4 + 101), f(g/4 + 101), f(b/4 + 101), dupe( displayObject ) );

		button.x = x;
		button.y = y;

		button.filters = Glassing.DROP_FILTER;

		if ( null != action && null != text ) {
			var thiz = button;
			button.addEventListener( 
				MouseEvent.CLICK
				, function( e ) { 
					if ( thiz.enabled ) {
						action( text, e ); 
					}
				} 
			);
		}

		return button;
	}

	public function enable() {
		this.enabled = true;
		this.upState = real_up;
	}

	public function disable() {
		this.enabled = !true;
		this.upState = this.disabled;//downState;
	}

	public function setEnabled( b ) {
		if ( b ) enable(); else disable();
	}
}
